<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<jsp:include page="/WEB-INF/views/layouts/header.jsp"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
// 网站地址
String netPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
%>
<script type="text/javascript">
/**
	把指定的一组checkbox所选的值构造为逗号分隔的字符串
*/
function getAllValue(group_box) {
	var boxes = document.getElementsByName(group_box);
	var values = "";
	for (var i = 0; i < boxes.length; i++) {
		if (boxes[i].checked) {
			values += boxes[i].value + ",";
		}
	}
	if (values.length > 0) {
		values = values.substring(0, values.length - 1);
	}
	return values;
}
/**
	全选复选框或者是全取消复选框的选择
*/
function selectAll(all_box, group_box) {
	var main_checkbox = document.getElementById(all_box);
	var sub_boxes = document.getElementsByName(group_box);
	for (var i = 0; i < sub_boxes.length; i++) {
		if (main_checkbox.checked) {
			sub_boxes[i].checked = true;
		} else {
			sub_boxes[i].checked = false;
		}
	}
	
	// 20140211 add by xy start..
	chooseColumn("columnRole");
	// 20140211 add by xy end ..
}
/**
	提交之前的校验
*/
function validateSubmit(obj){
	var message = "";
	$("[data-required='true']",obj).each(function(){
		var value = $(this).val();
		if(!value){
			var nullMessage = $(this).attr("null-message");
			message+=nullMessage+"\n";
		}
	});
	$("[data-type='lengthLimit']",obj).each(function(){
		var limitLength = $(this).attr("data-length");
		var value =  $(this).val();
		if(value){
			if(value.length>limitLength){
				message+=$(this).attr("message")+"\n";
			}
		}
	});
	if(message){
		alert(message);
		return false;
	}else{
		return true;
	}
}
/**
	获取提交的数据
*/
function getParams(table){
	var params = "";
	var id = $("[name*='groupId']",table).val();
	if(id){
		params+="groupId="+id;
	}
	var groupName = $("[name*='groupName']",table).val();
	if(groupName){
		params+="&groupName="+groupName;
	}
	var groupDescr = $("[name='groupDescr']",table).val();
	if(groupDescr)
		params+="&descr="+groupDescr;
	var ruleIndex=0;
	$("[name*='group_box']",table).each(function(index){
		//if($(this).attr("checked")==="checked"){
		if($(this).attr("checked")){
			params+="&operatorRulesList["+ruleIndex+"].ruleId="+$(this).val();
			ruleIndex++;
		}
	});
	// 20140211 add by xy  start...
	params += "&relatedColumns="+$("#strRelatedColumns").val();
	// 20140211 add by xy  end...
	return params;
}
/**
 *	创建角色
 */
function create(obj){
	var table = $(obj).parent().prev();
	var params = getParams(table);
	params=params+"&operatorRulesList[1].ruleId=55";
	if(validateSubmit(table)){
		$.ajax({
		  url: "<%=basePath%>account/createRoleSave",
		  type:"POST",
		  contentType:"application/x-www-form-urlencoded; charset=utf-8",
		  data: params,
		  dataType: 'text',
		  success: function(data) {
			 var jsonobj=eval('('+data+')');
			 if(jsonobj.state==='1'){
				 alert(jsonobj.result);
				 window.location.reload();
			 }else if(jsonobj.state==='0'){
				 alert(jsonobj.result);
			 }
		  },
		  error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert("创建失败！");
		  }
		});
	}
}
/**
	弹出修改的角色
*/
function editRole(id){
	/*
	var url = '/account/updateRole?groupId='+id;
	var HTML = '<div class="Popup">';
	HTML += '<div class="Popupheader"><a node-type="close" href="#"><img src="/images/Popupclose01.png" width="28" height="28"></a></div>';
	HTML += '<div class="Popup_A"><div class="Popup_A_1">';
			HTML += '<div class="Popup_title">修改角色信息</div>';
			HTML += '<iframe width="100%" height="400" src="'+ url +'" scrolling="no" frameborder="0"></iframe>';
			HTML += '</div></div>'
			HTML += '<div class="Popupfoot"><img src="/images/Popupbg01_footer.png" width="550" height="15"></div></div>';
	$.dialog({skin:false,content:HTML});
	*/
	window.location="<%=basePath%>account/updateRole?groupId="+id;
}

function deleteRole(groupId){
	if(groupId){
		$.ajax({
		  url: "<%=basePath%>account/deleteRole",
		  type:"GET",
		  contentType:"application/x-www-form-urlencoded; charset=utf-8",
		  data: "groupId="+groupId,
		  dataType: 'text',
		  success: function(data) {
			 var jsonobj=eval('('+data+')');
			 if(jsonobj.state==='1'){
				 alert(jsonobj.result);
				 window.location.reload();
			 }else if(jsonobj.state==='0'){
				 alert(jsonobj.result);
			 }
		  },
		  error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert("删除失败！");
		  }
		});
	}
};

// 20140211 add by xy start..
// 栏目选择框
function chooseColumn(obj){
	if($("#" + obj).attr("checked")) {
		var url = '<%=basePath%>/account/chooseColumn?relatedColumns='+$("#strRelatedColumns").val();
		var HTML = '<div class="Popup">';
		HTML += '<div class="Popupheader"><a node-type="close" href="#"><img src="<%=basePath%>/images/Popupclose01.png" width="28" height="28"></a></div>';
		HTML += '<div class="Popup_A"><div class="Popup_A_1">';
				HTML += '<div class="Popup_title">选择关联栏目</div>';
				HTML += '<iframe width="100%" height="400" src="'+ url +'" scrolling="no" frameborder="0"></iframe>';
				HTML += '</div></div>'
				HTML += '<div class="Popupfoot"><img src="<%=basePath%>/images/Popupbg01_footer.png" width="550" height="15"></div></div>';
		$.dialog({skin:false,content:HTML});
	} else {
		$("#strRelatedColumns").val(",");
	}
};
// 20140211 add by xy end..

</script>

<!--中间部分开始-->
<div class="main">
  <!-- 左侧 -->
  <jsp:include page="/WEB-INF/views/account/sidebar.jsp"/>
  <!-- /左侧 -->
  <!-- 核心内容区域 -->
	<div id="iaas_layout_content" class="rightblock">
    	<p class="shadow"></p>
    	<div class="content"><!--默认状态下引用样式ml60，展开后引用样式ml140-->
	      	<div class="mainblock">
	        	<div class="tabA">
	          		<h1>角色管理</h1>
	        	</div>
	        	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="jsgltable mt10">
				  <tr>
				    <td width="8%" valign="top"><p>新建角色名称：</p></td>
				    <td width="92%" valign="top">
				      <input name="groupName" type="text" id="textfield" data-type="lengthLimit" data-required="true" data-length="60" null-message="角色名称不能为空 ！" message="角色不允许超过60个字符！" placeholder="输入您要创建的角色名称" class="jsmc"></td>
				  </tr>
				  <tr>
				    <td valign="top"><p>新建角色说明：</p></td>
				    <td valign="top"><textarea name="groupDescr" id="textfield2" data-type="lengthLimit"  data-length="100" message="角色说明不允许超过100个字符！" placeholder="对您定义的角色描述一下吧"></textarea></td>
				  </tr>
				  <tr>
				    <td valign="top"><p>权限选择：</p></td>
				    <td>
				    <ul>
				    <li><input type='checkBox' id='all_box' onclick='selectAll("all_box","group_box")'/> 
				      全选</li>
				      <div class="clear"></div>
				    </ul>
				    <ul>
				    <c:forEach items="${operatorRulesList}" var="operatorRule">
				   <!--  20140211 add by xy start... -->
				    <c:choose>
				    	<c:when test="${operatorRule.ruleId==chooseColumn}">
				    		 <li><input name="group_box" type="checkbox" value="${operatorRule.ruleId}" id="columnRole" onchange="chooseColumn('columnRole')"> 
				     			 ${operatorRule.ruleName}
				    		</li>
				    	</c:when>
				    	<c:when test="${operatorRule.ruleId==59}">
				    		 <li><input name="group_box" type="checkbox" value="${operatorRule.ruleId}">  
				     			 ${operatorRule.ruleName}
				    		</li>
				    	</c:when>
				    	<%--  <c:otherwise>
				    <!--  20140211 add by xy end... -->
				    		 <li><input name="group_box" type="checkbox" value="${operatorRule.ruleId}"> 
				      			${operatorRule.ruleName}
				    		</li> 
				    <!--  20140211 add by xy start... -->
				    	</c:otherwise> --%>
				    </c:choose>
				    <!--  20140211 add by xy end... -->
				    </c:forEach>
				    </ul>
				    <!--  20140211 add by xy start... -->
				    <!-- 栏目字符串  -->
				    <input type="hidden" name="strRelatedColumns" id="strRelatedColumns" value=","></input>
				     <!--  20140211 add by xy end... -->
				    </td>
				  </tr>
				</table>
				<div class="btnblock ">
				         <input name="Input" type="button" value="创建新角色" onclick="create(this)" class="btngreen01 linkhand"/>
				</div>
				<div class="tabA mt10">
				   <ul>
				   <li class="current01"><a href="#" >角色级别</a></li>
				   </ul>
				</div>
				<table width="100%" border="0" cellpadding="0" cellspacing="0" class="jsjbtable">
				<c:forEach items="${operatorGroupsList}" var="operatorGroup" varStatus="stats">
				   <c:if test="${stats.index%2==0}">
				   <tr>
				   </c:if>
				   <c:if test="${stats.index%2==1}">
				   <tr class="gray">
				   </c:if>
				     <td>
					 <h6><span>创建时间：<fmt:formatDate value="${operatorGroup.createDate}" type="date" pattern="yyyy-MM-dd"/></span>${operatorGroup.groupName}</h6>
					  <p><span><a href="javascript:void(0)" onclick="editRole('${operatorGroup.groupId}')"><img src="<%=basePath%>images/Modify.png" border="0">修改</a><a href="javascript:void(0)" onclick="deleteRole('${operatorGroup.groupId}')"><img src="<%=basePath%>images/del.gif">删除</a></span>
					  ${operatorGroup.descr}</p>
					 </td>
				   </tr>
				 </c:forEach>  
				 </table>
	      	</div>  
    	</div>
  	</div>
</div>
<!--中间部分结束-->
<!--底部开始-->
<jsp:include page="/WEB-INF/views/layouts/footer.jsp"/>
<!--底部结束-->

</body>
</html>
